package com.lzm.listendemo;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;

public class SplashActivity extends AppCompatActivity {
    private Button btn;
    private ImageView iv_splash;
    private ImageView iv_splash2;
    private CountDownTimer timer;

    private String pic = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcdn.duit" +
            "ang.com%2Fuploads%2Fitem%2F201411%2F29%2F20141129212359_cLvTh.thumb.700_0.jpeg" +
            "&refer=http%3A%2F%2Fcdn.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0" +
            "n&fmt=jpeg?sec=1625123741&t=f26e0761af9667623baf91b36ed84306";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        btn = findViewById(R.id.btn_skip);
        iv_splash = findViewById(R.id.iv_splash);
        iv_splash2 = findViewById(R.id.iv_splash2);
        if (Build.VERSION.SDK_INT >= 28) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
        Glide.with(this)
                .load(pic)
                .into(iv_splash);
        btn.setOnClickListener(v -> {
            timer.onFinish();
        });
        timer = new CountDownTimer(2000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                runOnUiThread(() -> {
                    btn.setText((millisUntilFinished / 1000) + "s");
                });
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                overridePendingTransition(R.anim.act_in, R.anim.act_out);
                startActivity(intent);
                timer.cancel();
                timer = null;
                finish();
            }
        }.start();
    }

    public Bitmap getBlackImage(Bitmap bm) {
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.RGB_565);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Canvas canvas = new Canvas(bmp);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bm, 0, 0, paint);
        canvas.drawColor(Color.parseColor("#90000000"));
        return bmp;

    }

}